<template>
  <div class="kw-form-item" :class="config.cls">
    <div class="kw-form-item-label">
      <span>{{config.name}}</span>
      <span class="desc">{{config.desc}}</span>
    </div>
    <div class="kw-form-item-content">
      <div v-bind:class="{limitdel:defaultValue.length<=1,limitadd:defaultValue.length>=200}">
        <div v-for="(item, index) in defaultValue" class="kw-form-option" :key="index">
          <input type="text" maxlength="20" v-model="item.text">
          <a @click="(e)=>{del(e, item, index)}" class="action action-del">
            <i class="iconfont icon-minus"></i>
          </a>
          <a @click="(e)=>{add(e, item, index)}" class="action action-add">
            <i class="iconfont icon-plus"></i>
          </a>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { prop } from '../mixins/prop'
export default {
  name: 'options-prop',
  mixins: [prop],
  props: {
    setting: {
      default() {
        return {
          options: []
        }
      }
    },
    config: {
      default: {
        prop: 'options'
      }
    }
  },
  data() {
    return {
      defaultValue: this.setting.options || [],
      optionLabel: '选项'
    }
  },
  methods: {
    add(e, item, index) {
      e.stopPropagation()
      e.preventDefault()
      this.defaultValue.splice(index + 1, 0, {text: this.optionLabel + (index + 2)})
    },
    del(e, item, index) {
      e.stopPropagation()
      e.preventDefault()
      this.defaultValue.splice(index, 1)
    }
  }
}
</script>

<style lang="less">
  .limitadd .action-add,
  .limitdel .action-del {
    opacity: 0.2;
    pointer-events: none;
  }
  .action {
    .iconfont {
      background: #3c3435;
      border-radius: 50%;
      color: #fff;
      padding: 2px;
      margin: 0 3px;
      &:hover {
        background: #000;
      }
    }
  }
</style>